<template>
  <div>
    <!-- 搜索表单 -->
    <div style="text-align: center">
      <el-input style="width: 240px;" placeholder="请输入名称" v-model="params.name"></el-input>
      <el-input style="width: 240px; margin-left: 5px;" placeholder="请输入联系方式" v-model="params.phone"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search"
                                                                          style="margin-right: 5px"></i>搜索
      </el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"
                                                                           style="margin-right: 5px"></i>重置
      </el-button>
    </div>
    <!-- 用户详细数据 -->
    <el-table :data="tableData" :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}">
      <el-table-column prop="name" label="用户名"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="phone" label="电话"></el-table-column>
      <el-table-column prop="balance" label="余额"></el-table-column>
      <el-table-column label="状态">
        <template v-slot="scope">
          <el-switch
              v-model="scope.row.status"
              @change="changeStatus(scope.row)">
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间"></el-table-column>

      <el-table-column label="操作" width="250px">
        <template v-slot="scope">
          <!-- scope.row就是当前行数据 -->
          <el-button type="primary" @click="$router.push('/editUser?id=' + scope.row.id )">编辑</el-button>
          <!-- 确认删除 -->
          <el-popconfirm
              title="您确定要删除该用户吗？"
              style="margin:0 5px;"
              @confirm="del(scope.row.id)">
            <el-button slot="reference" type="danger">删除</el-button>
          </el-popconfirm>
          <!-- 重置密码 -->
          <el-button slot="reference" type="warning" @click="HandleChangePassword(scope.row)">修改密码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div>
      <el-pagination style="margin-top: 10px; text-align: center" background :total="total" :page-size="params.pageSize"
                     :current-page="params.pageNum" layout="prev,pager,next" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <!-- 弹窗 -->
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible">
      <el-form :model="form" label-width="100px" ref="formRef" :rule="rules">
        <el-form-item label="新的密码" prop="newPassword">
          <el-input v-model="form.newPassword" autocomplete="off"  show-password></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="savePassword">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'User',
  data() {
    return {
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
      params: {
        pageNum: 1,
        pageSize: 7,
        name: '',
        phone: '',
      },
      rules: {
        newPassword: [
          {required: true, message: '请输入新的密码', trigger: 'blur'},
          {min: 6, max: 25, message: '长度在6-25位', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    this.load()
  },
  methods: {
    HandleChangePassword(row) {
      this.dialogFormVisible = true;
      this.form = JSON.parse(JSON.stringify(row));

    },
    changeStatus(row) {
      request.put('/user/', row).then(res => {
        if (res.code === '200') {
          this.$notify.success('操作成功');
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      })
    },
    savePassword(){
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          request.put('/user/password',this.form).then(res => {
            if (res.code == '200') {
              this.$notify.success("修改成功");
              this.load();
              this.dialogFormVisible = false;
            }else {
              this.$notify.success("修改失败");
            }

          })
        }
      })
    },
    load() {
      request.get("/user/page", {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 7,
        name: '',
        phone: '',
      }
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },
    del(id) {
      request.delete("/user/" + id).then(res => {
        if (res.code === '200') {
          this.$notify.success('删除成功')
          this.load()
        } else {
          this.$notify.error(res.msg)
        }
      })
    }
  }
}
</script>

<style scoped>


</style>